package com.whut.whw.setting;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.util.EmptyUtils;

import java.util.List;

/**
 * <pre>
 *  @Decription 用于基本项的展示
 *  @datetime 2017/06/26 15:28
 *  @author 忘尘无憾
 *  @version
 * </pre>
 */
public class SettingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<BaseItemModel> itemModelList;
    private Context context;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_SPLIT = 2;

    private OnItemClickListener onItemClickListener;

    /**
     * 构造函数
     *
     * @param context
     * @param itemModelList
     */
    public SettingAdapter(Context context, List<BaseItemModel> itemModelList) {
        this.context = context;
        this.itemModelList = itemModelList;
    }

    public interface OnItemClickListener {
        void OnItemClick(View view, int position);

        void OnItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.base_item, parent, false);
            return new BaseItemHolder(view);
        }
        if (viewType == TYPE_SPLIT) {
            View view = LayoutInflater.from(context).inflate(R.layout.base_item, parent, false);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(view.getLayoutParams());
            //列表项之间隔开
            lp.setMargins(0, 0, 0, 40);
            view.setLayoutParams(lp);
            view.setBackgroundResource(R.drawable.border_top_bottom);
            return new BaseItemHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BaseItemHolder) {
            setListen(holder);
            setBaseData(holder, position);
        }
    }

    /**
     * 设置监听
     * @param holder
     */
    private void setListen(final RecyclerView.ViewHolder holder) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getLayoutPosition();
                onItemClickListener.OnItemClick(holder.itemView, position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int position = holder.getLayoutPosition();
                onItemClickListener.OnItemLongClick(holder.itemView, position);
                return true;
            }
        });
    }

    /**
     * 设置每行的左右图标和标题
     *
     * @param holder
     * @param position
     */
    private void setBaseData(RecyclerView.ViewHolder holder, int position) {
        BaseItemModel itemModel = this.itemModelList.get(position);
        BaseItemHolder itemHolder = (BaseItemHolder) holder;
        if (EmptyUtils.isEmpty(itemModel.getLeftImg())) {//如果左边传输的不是图片路径
            Glide
                    .with(this.context)
                    .load(itemModel.getLeftIcon())
                    .into(itemHolder.item_left_iv);
        } else {
            Glide
                    .with(this.context)
                    .load(itemModel.getLeftImg())
                    .into(itemHolder.item_left_iv);
        }
        if (EmptyUtils.isEmpty(itemModel.getRightImg())) {//如果右边传输的不是图片路径
            Glide
                    .with(this.context)
                    .load(itemModel.getRightIcon())
                    .into(itemHolder.item_right_iv);
        } else {
            Glide
                    .with(this.context)
                    .load(itemModel.getRightImg())
                    .into(itemHolder.item_right_iv);
        }
        itemHolder.item_title_tv.setText(itemModel.getTitle());
    }

    @Override
    public int getItemCount() {
        return itemModelList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return TYPE_SPLIT;
        }
        return TYPE_ITEM;
    }

    /**
     * <pre>
     *  @Decription
     *  @datetime 2017/06/26 15:53
     *  @author 忘尘无憾
     *  @version
     * </pre>
     */
    private class BaseItemHolder extends RecyclerView.ViewHolder {

        private ImageView item_left_iv;
        private TextView item_title_tv;
        private ImageView item_right_iv;

        public BaseItemHolder(View itemView) {
            super(itemView);
            item_left_iv = (ImageView) itemView.findViewById(R.id.item_left_iv);//左图标
            item_title_tv = (TextView) itemView.findViewById(R.id.item_title_tv);//文本标题
            item_right_iv = (ImageView) itemView.findViewById(R.id.item_right_iv);//右图标
        }
    }
}
